package com.decent.ejfadmin.product.dao.provider;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.product.request.ModelPriceParam;
import com.decent.ejfadmin.product.request.ProductQueryParam;
import com.decent.ejfadmin.product.request.UserPriceParam;
import com.decent.ejfadmin.utils.SQLWrapUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchao
 * @date 2018/7/25 11:19
 */
public class ProductDaoProvider {

	public String queryProductPage(HashMap<String, Object> params) {
		StringBuffer sql = new StringBuffer(100);
		sql.append("SELECT\n" + "\tp.id,\n" + "p.product_name,\n" + "p.product_type,\n" + "p.area,\n" + "p.face,\n"
				+ "p.type,\n" + "p.state,\n" + "p.modified,\n" + "p.admin,\n" + "p.card_product_id,\n" + "p.third_id,\n"
				+ "p.submit_type,\n" + "p.card_restrict,\n" + "p.ticket_spuid ticketSpuid," + "p.spu_status spuStatus,"
				+ " p.remark, \n" + "o.tipText,\n" + "o.card_operator_name " + "FROM\n"
				+ "\tyuhuang_supply_product p,\n" + "\tyuhuang_card_operator o\n" + "WHERE\n"
				+ "\tp.product_type = o.card_operator_id ");
		ProductQueryParam param = (ProductQueryParam) params.get("param");
		if (null != param.getId()) {
			sql.append(" and p.id = #{id} ");
		}
		if (StringUtils.isNotBlank(param.getProductName())) {
			sql.append(" and instr(p.product_name , #{param.productName})>0 ");
		}
		if (StringUtils.isNotBlank(param.getCardProductId())) {
			sql.append(" and card_product_id=#{param.cardProductId} ");
		}
		if (null != param.getProductType()) {
			sql.append("and p.product_type = #{param.productType} ");
		}
		if (StringUtils.isNotBlank(param.getArea())) {
			sql.append("and p.area = #{param.area} ");
		}
		if (null != param.getFace()) {
			sql.append("and p.face = #{param.face} ");
		}
		if (null != param.getType()) {
			sql.append("and p.type = #{param.type} ");
		}
		if (null != param.getState()) {
			sql.append("and p.state = #{param.state} ");
		}
		if (null != param.getAdmin()) {
			sql.append("and p.admin = #{param.admin} ");
		}
		if (Objects.nonNull(param.getTicketSpuid())) {
			sql.append("and p.ticket_spuid = #{param.ticketSpuid} ");
		}
		if (Objects.nonNull(param.getSpuStatus())) {
			sql.append("and p.spu_status = #{param.spuStatus} ");
		}
		sql.append(" order by p.type asc,p.product_type asc,p.face desc");
		Page page = param.getPageUnit();
		return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
	}

	public String queryModelPricePage(HashMap<String, Object> params) {
		StringBuffer sql = new StringBuffer(200);
		String orgiSql = "select ysm.model_name,ysmpm.min_price,ysmpm.max_price,ysp.product_name,ysmpm.state,"
				+ "ysp.product_type,ysp.area,ysp.face,ysp.type,o.card_operator_name" + " from yuhuang_supply_model ysm "
				+ "inner join yuhuang_supply_model_price_map ysmpm on ysm.id = ysmpm.model_id "
				+ "inner join yuhuang_supply_product ysp on ysmpm.product_id = ysp.id "
				+ "INNER JOIN yuhuang_card_operator o ON ysp.product_type = o.card_operator_id  where 1=1 ";
		sql.append(orgiSql);
		ModelPriceParam param = (ModelPriceParam) params.get("param");

		if (null != param.getSupplyModel()) {
			sql.append("AND  ysm.id = #{param.supplyModel} ");
		}
		if (org.apache.commons.lang3.StringUtils.isNotBlank(param.getProductName())) {
			sql.append("AND  ysp.product_name = #{param.productName} ");
		}
		if (null != param.getProductType()) {
			sql.append("and ysp.product_type = #{param.productType} ");
		}
		if (StringUtils.isNotBlank(param.getArea())) {
			sql.append("and ysp.area = #{param.area} ");
		}
		if (null != param.getFace()) {
			sql.append("and ysp.face = #{param.face} ");
		}

		Page page = param.getPageUnit();
		return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
	}

	public String queryUserPricePage(HashMap<String, Object> params) {
		StringBuffer sql = new StringBuffer(200);
		String orgiSql = "select yupp.id,IFNULL(yupp.xianyu_price,0) xianyuPrice,yupp.min_price,yupp.max_price,ysp.product_name,yupp.state,"
				+ "ysp.product_type,ysp.area,ysp.face,ysp.type,ysu.account,ysi.name userName,"
				+ "o.card_operator_name,yupp.state,yupp.modify_time modifytime,yupp.remark,"
				+ " case when o.card_type_id <= 3 then ifNull(yupp.max_price * ysp.face * yupp.xianyu_price, 0) "
				+ " else ifNull(yupp.max_price * yupp.xianyu_price, 0) end as xianyuRebate "
				+ " from yuhuang_user_product_price yupp "
				+ "inner join yuhuang_supply_product ysp on yupp.product_id = ysp.id "
				+ "inner join yuhuang_sup_user ysu on yupp.user_id = ysu.user_id "
				+ "inner join yuhuang_sup_info ysi on yupp.user_id = ysi.user_id "
				+ "INNER JOIN  yuhuang_card_operator o  ON ysp.product_type = o.card_operator_id where 1=1 ";
		sql.append(orgiSql);
		UserPriceParam param = (UserPriceParam) params.get("param");

		if (StringUtils.isNotBlank(param.getProductName())) {
			sql.append("AND  ysp.product_name = #{param.productName} ");
		}
		if (null != param.getProductType()) {
			sql.append("and ysp.product_type = #{param.productType} ");
		}
		if (null != param.getFace()) {
			sql.append("and ysp.face = #{param.face} ");
		}
		if (StringUtils.isNotBlank(param.getArea())) {
			sql.append("and ysp.area = #{param.area} ");
		}
		if (org.apache.commons.lang3.StringUtils.isNotBlank(param.getUserAccount())) {
			sql.append("AND  (instr(ysu.account, #{param.userAccount})>0 or instr(ysi.name, #{param.userAccount})>0) ");
		}
		if (Objects.nonNull(param.getState())) {
			sql.append(" AND yupp.state=#{param.state} ");
		}
		if (Objects.nonNull(param.getMinDiscount())) {
			sql.append(" AND yupp.max_price>=#{param.minDiscount} ");
		}
		if (Objects.nonNull(param.getMaxDiscount())) {
			sql.append(" and yupp.max_price<=#{param.maxDiscount} ");
		}
		System.out.println("ProductDaoPrivider L155-->\n" + sql);
		Page page = param.getPageUnit();
		return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
	}

	public String queryAllModel(HashMap<String, Object> params) {
		StringBuffer sql = new StringBuffer(200);
		sql.append("select id ,model_name modelName,remark from yuhuang_supply_model ");
		if ((Boolean) params.get("param")) {
			sql.append(" where id not in(23649,23650)");
		}
		return sql.toString();
	}

	public String updateCardRestrict(HashMap<String, Object> params) {
		StringBuilder sql = new StringBuilder(100);
		List<Long> productIds = (List<Long>) params.get("productIds");
		sql.append(
				" update yuhuang_supply_product set modified = now(),admin=#{name},card_restrict = #{cardRestrict} where id in ( ");
		sql.append(StringUtils.join(productIds, ","));
		sql.append(" )");
		return sql.toString();
	}

	/**
	 * 清除元目标产品密价
	 *
	 * @param params
	 * @return
	 */
	public String getClearProductId(HashMap<String, Object> params) {
		Integer productType = (Integer) params.get("productType");
		StringBuilder sql = new StringBuilder(100);
		sql.append("select b.product_id from yuhuang_supply_product a ,yuhuang_user_product_price b "
				+ "where a.id=b.product_id and b.user_id=#{targetId} ");
		if (Objects.nonNull(productType)) {
			sql.append(" and a.product_type=#{productType} ");
		}
		return sql.toString();
	}

	public static void main(String[] args) {
		ProductDaoProvider p = new ProductDaoProvider();
		HashMap<String, Object> params = new HashMap<>();
		params.put("param", new UserPriceParam());
		p.queryUserPricePage(params);
	}
}
